package com.lg.system.main.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lg.common.base.BaseController;
import com.lg.common.validate.Delete;
import com.lg.common.validate.Insert;
import com.lg.common.validate.Update;
import com.lg.system.main.entity.Dict;
import com.lg.system.main.service.DictService;
import com.lg.utils.PageModel;
import com.lg.utils.Query;
import com.lg.utils.R;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author dl
 * @since 2018-04-02
 */
@RestController
@RequestMapping("/dict")
public class DictController extends BaseController<Dict, DictService> {

    private final String prefix="system/main/dict/";
    
	@Autowired
	DictService dictService;

	@RequiresPermissions("dict:dict")
	@GetMapping("/index")
	public ModelAndView index(){
		ModelAndView mView=new ModelAndView(prefix+"index");
		return mView;
	}
	
	@RequiresPermissions("dict:dict")
	@RequestMapping("/list")
	public PageModel selectList(@RequestBody Dict  dict){
		Query<Dict> query = new Query<Dict>(dict);
		List<Dict> dicts=dictService.selectList(query.getEntityWrapper());
		PageModel pa=new PageModel(dicts,query.getPage().getTotal());
		return pa;
	}
	
	@RequiresPermissions("dict:add")
	@GetMapping("/add/{id}")
	public ModelAndView add(@PathVariable("id")  Long id){
		Dict dict=new Dict();
		if(id.intValue()!=-1){
			dict=dictService.selectById(id);
		}
		ModelAndView mView=new ModelAndView(prefix+"add");
		mView.addObject("obj", dict);
		return mView;
	}
	
	@RequiresPermissions("dict:edit")
	@GetMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable("id") Long id){
		ModelAndView mView=new ModelAndView(prefix+"edit");
		mView.addObject("obj", dictService.selectById(id));
		return mView;
	}
	
	@RequiresPermissions("dict:add")
	@PostMapping("/save")
	public R save(@RequestBody @Validated(value={Insert.class}) Dict dict,BindingResult bindingResult){
		dict.setCreateId(getUserId());
		if(dictService.save(dict)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("dict:edit")
	@PostMapping("/update")
	public R update(@RequestBody @Validated(value={Update.class}) Dict dict,BindingResult bindingResult){
		dict.setModifiedId(getUserId());
		if(dictService.update(dict)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("dict:del")
	@PostMapping("/deleteRow")
	public R deleteRow(@RequestBody @Validated(value={Delete.class}) Dict dict,BindingResult bindingResult){
		
		if(dictService.deleteById(dict.getId())){
			return R.ok();
		}else{
			return R.error();
		}  
	}
	
	@RequiresPermissions("dict:del")
	@PostMapping("/deleteRows")
	public R deleteRows(@RequestParam(value="ids[]") List<Long> ids){
		if(dictService.deleteByIds(ids)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@PostMapping("/type")
	public List<Dict> selectType(){
		EntityWrapper<Dict>  wrapper = new EntityWrapper<Dict>();
		wrapper.eq("del_flag", 0);
		wrapper.groupBy("type");
		List<Dict> dicts=dictService.selectList(wrapper);
		return dicts;
	}

}
